﻿using Assets.Game.scripts.components;
using Assets.Game.scripts.ui.models;
using Engine.Entities.Character;
using Game.Scripts.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Assets.Game.scripts.ui.controllers {

    [RequireComponent(typeof(SelectedBuildingModel))]
    public class BuildingCompositionWidgetController : MonoBehaviour {

        public UIGrid employedGrid;
        public UIGrid unemployedGrid;
        public UIScrollView employedScrollView;
        public UIScrollView unemployedGridScrollView;
        public GameObject employedItemRenderer;
        public GameObject unemployedItemRenderer;
        private bool isRedrawRequired;

        void Start() {
            SelectedBuildingModel model = this.GetComponent<SelectedBuildingModel>();

            if (model != null && this.employedGrid != null && this.unemployedGrid != null) {
                //aggiungo i personaggi senza lavoro
                if (this.unemployedItemRenderer != null) {
                    IEnumerable<SteveBrain> characters =
                        from SteveBrain brain in FrenzyPeopleController.singleton.SteveList
                        where brain.workplace == null
                        select brain;

                    foreach (SteveBrain unemployedCharacter in characters) {
                        SteveModel agentModel = unemployedCharacter.model;
                        GameObject itemRendererInstance = NGUITools.AddChild(
                            this.unemployedGrid.gameObject,
                            this.unemployedItemRenderer
                        );

                        CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                        if (itemModel != null) {
                            itemModel.data = unemployedCharacter;
                        }

                        UIDragDropItemListener dragListener = itemRendererInstance.GetComponent<UIDragDropItemListener>();
                        EventDelegate dragEndCallback = new EventDelegate(this, "onDragEnd");
                        dragEndCallback.parameters[0].value = itemRendererInstance;
                        dragEndCallback.parameters[1].value = itemModel;
                        if (dragListener != null) {
                            EventDelegate.Set(dragListener.onDragEnd, dragEndCallback);
                        }
                    }
                }

                //aggiungo i lavoratori dell'edificio
                if (this.employedItemRenderer != null) {
                    foreach (SteveBrain character in model.building.Workers) {
                        SteveModel agentModel = character.model;
                        GameObject itemRendererInstance = NGUITools.AddChild(
                            this.employedGrid.gameObject,
                            this.employedItemRenderer
                        );

                        CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                        if (itemModel != null) {
                            itemModel.data = character;
                        }

                        UIDragDropItemListener dragListener = itemRendererInstance.GetComponent<UIDragDropItemListener>();
                        EventDelegate dragEndCallback = new EventDelegate(this, "onDragEnd");
                        dragEndCallback.parameters[0].value = itemRendererInstance;
                        dragEndCallback.parameters[1].value = itemModel;
                        if (dragListener != null) {
                            EventDelegate.Set(dragListener.onDragEnd, dragEndCallback);
                        }
                    }
                }
            }

            this.isRedrawRequired = true;
        }

        void Update() {
            if (this.isRedrawRequired) {
                if (this.employedGrid != null && this.employedScrollView != null) {
                    this.employedScrollView.ResetPosition();
                    this.employedScrollView.UpdatePosition();
                    this.employedGrid.Reposition();
                }

                if (this.unemployedGrid != null && this.unemployedGridScrollView != null) {
                    this.unemployedGridScrollView.ResetPosition();
                    this.unemployedGridScrollView.UpdatePosition();
                    this.unemployedGrid.Reposition();
                }

                this.isRedrawRequired = false;
            }
        }

        public void onClose() {
            NGUITools.Destroy(this.gameObject);
        }


        public void onDragEnd(GameObject draggedItem, CharacterItemRendererModel item) {
            Transform parent = draggedItem.transform.parent;
            bool isDraggedOnEmployedGrid = System.Object.ReferenceEquals(parent, this.employedGrid.transform);
            bool isDraggedOnUnemployedGrid = System.Object.ReferenceEquals(parent, this.unemployedGrid.transform);

            if (parent.gameObject != null) {
                SelectedBuildingModel model = this.GetComponent<SelectedBuildingModel>();
                if (isDraggedOnEmployedGrid) {
                    //aggiungo l'individuo all'edificio entro la sua capienza
                    if (!model.building.isFull) {
                        model.building.addWorker(item.data);
                    } else {
                        //riposiziono l'individuo fra i disoccupati
                        GameObject child = parent.transform.GetChild(parent.transform.childCount - 1).gameObject;
                        this.unemployedGrid.AddChild(child.transform);
                        NGUITools.MarkParentAsChanged(child);
                    }
                } else if (isDraggedOnUnemployedGrid) {
                    //rimuovo l'individuo dall'edificio
                    model.building.removeWorker(item.data);
                }
            }
        }
    }
}
